package com.leetcode.algorithm.y22.m09.w2;

/**
 * 714. 买卖股票的最佳时机含手续费
 * 
 * https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
 * 
 * @author jie.deng
 *
 */
class Question0714Solution02 {

	// 贪心
	public int maxProfit(int[] prices, int fee) {
		int profit = 0;
		int minPrice = prices[0];
		for (int i = 1; i < prices.length; i++) {
			if (prices[i] < minPrice) {
				// 买入
				minPrice = prices[i];
			} else if (prices[i] > minPrice + fee) {
				// 获取利润，但有可能不是真正的卖出
				profit += (prices[i] - minPrice - fee);
				minPrice = prices[i] - fee; // 关键的一步
			}
		}

		return profit;
	}

}